<?php

require "includes/class.template.php";
$template = new verySimpleTemplate();

switch( @$_POST['action'] )
{
	case 'qq':
	if( $_POST['qq'] ) { qq( $_POST['qq'] ); } else { def(); } break;

	case 'mobile':
	if( $_POST['mobile'] ) { mobile( $_POST['mobile'] ); } else { def(); } break;

	case 'birth':
	if( $_POST['month'] and $_POST['day'] ) { birth( $_POST['birth'] ); } else { def(); } break;

	default: def(); break;
}

function birth( $c )
{
	global $template;
	require "data/constellation.data.php";

	if( !@$_POST['month'] or !@$_POST['day'] )
	{
		$template->deal( 'template/header.tpl' );
		$h = $template->template;
		$template->deal( 'template/fortune.tpl' );
		$b = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $b . $f;
		exit();
	}
	else
	{
		$cV = @$_POST['month'] . '-' . @$_POST['day'];
		if( !in_array( $cV, $data ) )
		{
			error( '对不起,您输入的日期不正确,请重新输入。' );
			exit();
		}
	}

	if( file_exists( 'data/birth/' . $cV . '.txt' ) )
	{
		$c = file_get_contents( 'data/birth/' . $cV . '.txt' );
		$arrayVar = array();
		$template->deal( 'template/header.tpl' );
		$h = $template->template;
		$template->deal( 'template/fortune.tpl' );
		$b = $template->template;
		$arrayVar = array (
							'BIRTH'	=> $c
						  );
		$template->deal( 'template/birthRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $b . $r . $f;
	}
	else
	{
		error( '对不起,您输入的日期不正确,请重新输入。' );
	}
}

function mobile( $c )
{
	global $template;
	
	if( !@$_POST['mobile'] )
	{
		$template->deal( 'template/header.tpl' );
		$h = $template->template;
		$template->deal( 'template/fortune.tpl' );
		$b = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $b . $f;
		exit();
	}

	$mobile = @$_POST['mobile'] ;

	if( strlen( $mobile ) != 11 or !is_numeric( $mobile ) )
	{
		error( '对不起,你输入的手机号码无效.请重新输入.' );
		exit();
	}
	else
	{
		$li = intval( substr( $mobile, 2 ) ) % 80;
		$c = file( 'data/mobileLucky.dat' );
		$t = explode( '|', $c[$li] );
		$arrayVar = array();
		$template->deal( 'template/header.tpl' );
		$h = $template->template;
		$template->deal( 'template/fortune.tpl' );
		$b = $template->template;
		$arrayVar = array (
							'MOBILE'	=> $mobile,
							'LUCKY'		=> $t[1],
							'DES'		=> $t[0],
						   );
		$template->deal( 'template/mobileLuckyRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $b . $r . $f;
	}
}

function qq( $c )
{
	global $template;

	if( !@$_POST['qq'] )
	{
		$template->deal( 'template/header.tpl' );
		$h = $template->template;
		$template->deal( 'template/fortune.tpl' );
		$b = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $b . $f;
		exit();
	}
	else
	{
		$q = str_replace( '.', '', @$_POST['qq'] );
	}
	if( strlen( $q ) > 9 or strlen( $q ) < 5 or !is_numeric( $q ) or $q < 0 )
	{
		error( '对不起,你输入的QQ号码无效.请重新输入.' );
		exit();
	}
	else
	{
		$li = $q % 81;
		$c = file( 'data/luck.dat' );
		$t = explode( '|', $c[$li] );
		$arrayVar = array();
		$template->deal( 'template/header.tpl' );
		$h = $template->template;
		$template->deal( 'template/fortune.tpl' );
		$b = $template->template;
		$arrayVar = array (
							'QQ'	=> $q,
							'LUCKY'	=> $t[1],
							'DES'	=> $t[0],
							'TYPE'	=> $t[2]
						  );
		$template->deal( 'template/qRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $b . $r . $f;

	}
}


function def()
{
	global $template;

	$arrayVar = array();
	$template->deal( 'template/header.tpl' );
	$h = $template->template;
	$template->deal( 'template/fortune.tpl' );
	$b = $template->template;
	/*if( @$_GET['ipwhois'] )
	{
		$arrayVar = array ( 
							'LOCAL_IP'  => @$_GET['ipwhois'],
							'LOCALTION' => $l
						);
		$template->deal( 'template/ipLocalWhoisRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $r . $b . $f;
	}
	else
	{
		$arrayVar = array ( 
							'LOCAL_IP'  => $c,
							'LOCALTION' => $l
						);
		$template->deal( 'template/ipLocalRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $r . $b . $f;
	}*/
	$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $r . $b . $f;
}

function error( $e )
{
	global $template;
	$arrayVar = array();
	$template->deal( 'template/header.tpl' );
	$h = $template->template;
	$template->deal( 'template/fortune.tpl' );
	$b = $template->template;
	$arrayVar = array ( 
						'ERROR'  => $e
					  );
	$template->deal( 'template/errorRender.tpl', $arrayVar );
	$r = $template->template;
	$template->deal( 'template/footer.tpl' );
	$f = $template->template;
	echo $h . $r . $b . $f;
}
?>